﻿using System.Drawing;
using System.Linq;
using System.Windows.Controls;
using c2iGMailNotifier.ViewModel;
using GalaSoft.MvvmLight.Messaging;
using Microsoft.WindowsAPICodePack.Taskbar;

namespace c2iGMailNotifier.View
{
    /// <summary>
    /// Logique d'interaction pour EmailsListView.xaml
    /// </summary>
    public partial class EmailsListView
    {
        public EmailsListView()
        {
            InitializeComponent();
            Messenger.Default.Register(this, delegate(PropertyChangedMessage<bool> propertyChangedMessage)
                                                 {
                                                     if (propertyChangedMessage.NewValue)
                                                     {
                                                         TaskbarManager.Instance.TabbedThumbnail.InvalidateThumbnails();
                                                         TaskbarManager.Instance.SetProgressState(TaskbarProgressBarState.Indeterminate);
                                                     }
                                                     else
                                                     {
                                                         if (Model.HasError)
                                                             TaskbarManager.Instance.SetOverlayIcon(Properties.Resources.error, "An error occur");
                                                         else if (this.Model.AtomEntries.Count == 0)
                                                             TaskbarManager.Instance.SetOverlayIcon(Properties.Resources.MailCount0, "No unread message");
                                                         else
                                                             TaskbarManager.Instance.SetOverlayIcon(CreateIcon(this.Model.AtomEntries.Count()), string.Format("You have {0} unread messages", this.Model.AtomEntries.Count));

                                                         TaskbarManager.Instance.SetProgressState(TaskbarProgressBarState.NoProgress);

                                                         TaskbarManager.Instance.TabbedThumbnail.InvalidateThumbnails();
                                                     }
                                                 });
        }

        /// <summary>
        /// ViewModel associated with this View.
        /// </summary>
        private EmailsListViewModel Model
        {
            get { return this.DataContext as EmailsListViewModel; }
        }


        #region events
        /// <summary>
        /// Click on a "link" and call ViewModel's ExecuteNavigateCommand.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TextBlock_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            TextBlock txtLink = sender as TextBlock;
            if (txtLink != null && txtLink.Tag != null)
                this.Model.ExecuteNavigateCommand(txtLink.Tag.ToString());
        }
        #endregion

        /// <summary>
        /// Create a specific icon for W7 Taskbar. Add number of unread emails
        /// </summary>
        /// <param name="number"></param>
        /// <returns></returns>
        /// <remarks>This code was founded on Sergey Zwezdin blog, a MS MVP.
        /// (http://weblogs.asp.net/sergeyzwezdin/archive/2009/05/19/windows-7-programming-taskbar-part-4-custom-overlayicon.aspx)</remarks>
        private Icon CreateIcon(int number)
        {
            Bitmap bmp = Properties.Resources.MailCount.ToBitmap();
            using (Graphics g = Graphics.FromImage((bmp)))
            {
                using (StringFormat sf = new StringFormat())
                {
                    sf.Alignment = StringAlignment.Center;
                    sf.LineAlignment = StringAlignment.Center;
                    RectangleF rect = new RectangleF(0, 0, bmp.Width, bmp.Height);
                    g.DrawString(number.ToString(), new Font("Segoe UI", 8), Brushes.White, rect, sf);
                }
            }

            return Icon.FromHandle(bmp.GetHicon());
        }


    }
}